//-----------------------------------------------------------------------
// <copyright file="a_CatTreeDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_CatTreeDAOBase
    {
        #region Common methods
        public virtual a_CatTree Createa_CatTreeFromReader(IDataReader reader)
        {
            a_CatTree item = new a_CatTree();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) item.ID = (int)reader["ID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatID"))) item.CatID = (int)reader["CatID"];
                if (!reader.IsDBNull(reader.GetOrdinal("SubCatID"))) item.SubCatID = (int)reader["SubCatID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatSub2ID"))) item.CatSub2ID = (int)reader["CatSub2ID"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_CatTree methods
            
        public virtual void Createa_CatTree(a_CatTree a_CatTree)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatTree_ins");
                
                database.AddInParameter(dbCommand, "@CatID", DbType.Int32, a_CatTree.CatID);
                database.AddInParameter(dbCommand, "@SubCatID", DbType.Int32, a_CatTree.SubCatID);
                database.AddInParameter(dbCommand, "@CatSub2ID", DbType.Int32, a_CatTree.CatSub2ID);
                database.AddOutParameter(dbCommand, "@ID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_CatTree.ID = (int)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_CatTree methods
        
        public virtual void Updatea_CatTree(a_CatTree a_CatTree)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatTree_upd");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, a_CatTree.ID);
                database.AddInParameter(dbCommand, "@CatID", DbType.Int32, a_CatTree.CatID);
                database.AddInParameter(dbCommand, "@SubCatID", DbType.Int32, a_CatTree.SubCatID);
                database.AddInParameter(dbCommand, "@CatSub2ID", DbType.Int32, a_CatTree.CatSub2ID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_CatTree methods
        public virtual void Deletea_CatTree(int id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatTree_del");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_CatTree methods
        
        public virtual a_CatTree Geta_CatTree(int id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatTree_sel");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                a_CatTree a_CatTree =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_CatTree = Createa_CatTreeFromReader(reader);
                        reader.Close();
                    }
                }
                return a_CatTree;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_CatTree by Mcid and Scid methods

        public virtual a_CatTree Geta_CatTree_mcid_scid(int mcid,int scid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatTree_sel_mcid_scid");

                database.AddInParameter(dbCommand, "@CatID", DbType.Int32, mcid);
                database.AddInParameter(dbCommand, "@SubCatID", DbType.Int32, scid);

                a_CatTree a_CatTree = null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_CatTree = Createa_CatTreeFromReader(reader);
                        reader.Close();
                    }
                }
                return a_CatTree;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Geta_CatTreeList methods
        public virtual a_CatTreeCollection Geta_CatTreeList(a_CatTreeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatTree_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_CatTreeCollection a_CatTreeCollection = new a_CatTreeCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_CatTree a_CatTree = Createa_CatTreeFromReader(reader);
                        a_CatTreeCollection.Add(a_CatTree);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_CatTreeCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_CatTreeCollection Geta_CatTreeList(a_CatTreeColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_CatTreeList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion

        #region Geta_CatTreeList By CatMainId methods
        public virtual a_CatTreeCollection Geta_CatTreeList_mcid(Int32 mcid, a_CatTreeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatTree_selList_mcid");

                database.AddInParameter(dbCommand, "@CatID", DbType.Int32, mcid);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_CatTreeCollection a_CatTreeCollection = new a_CatTreeCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_CatTree a_CatTree = Createa_CatTreeFromReader(reader);
                        a_CatTreeCollection.Add(a_CatTree);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_CatTreeCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_CatTreeCollection Geta_CatTreeList_mcid(Int32 mcid, a_CatTreeColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_CatTreeList_mcid(mcid, orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion

        #region Geta_CatTreeList By SubCatId methods
        public virtual a_CatTreeCollection Geta_CatTreeList_scid(Int32 scid, a_CatTreeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatTree_selList_scid");

                database.AddInParameter(dbCommand, "@SubCatId", DbType.Int32, scid);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_CatTreeCollection a_CatTreeCollection = new a_CatTreeCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_CatTree a_CatTree = Createa_CatTreeFromReader(reader);
                        a_CatTreeCollection.Add(a_CatTree);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_CatTreeCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_CatTreeCollection Geta_CatTreeList_scid(Int32 scid, a_CatTreeColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_CatTreeList_scid(scid, orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion
    }
}